package net.lesscoding.general;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @author eleven
 * @date 2023/10/8 20:57
 * @apiNote 同步消息发送者
 */
public class SyncProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // - 创建消息生产者Producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // - 指定nameServer地址
        producer.setNamesrvAddr("192.168.33.129:9876");
        // 超时时间
        producer.setSendMsgTimeout(1000000);
        // 发送失败重试三次
        producer.setRetryTimesWhenSendFailed(3);
        // - 启动producer
        producer.start();

        for (int i = 0; i < 10; i++) {
            byte[] body = ("Hi," + i).getBytes();
            Message message = new Message("TopicTest", "someTag", body);
            SendResult sendResult = producer.send(message);
            System.out.println(sendResult);
        }
        // - 关闭生产者Producer
        producer.shutdown();
    }
}
